// firstPackage/pages/calculatorLoans/calculatorLoans.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    calculateType: [
      {
        title: "贷款总额",
        key: 0,
      },
      {
        title: "房屋总价",
        key: 1,
      },

    ],
    loansTypeArr: [
      {
        title: "商业贷",
        key: 0,
      },
      {
        title: "公积金贷",
        key: 1,
      },
      {
        title: "组合贷",
        key: 2,
      },
    ],
    //当前是按贷款总额还是按房屋总价:
    priceType: 0,//默认贷款总额
    //当前贷款方式:
    loansType: 0,//默认商业贷
    formData: {
      loansMoney: "",//贷款金额
      totalMoney: "",//房屋总价
      //首付比例
      payRatio: {
        label: "30%",
        value: 0.3
      },
      //商贷年限
      businessYear: {
        value: 30,
        label: "30年"
      },
      //公积金年限
      fundYear: {
        value: 30,
        label: "30年"
      },
      //利率方式:
      rateType: {
        value: "LPR",
        label: "使用最新LPR"
      },
      //利率
      rateValue: {
        value: 0.0465,
        label: "利率4.65% (LPR + 0基点)"
      },
      //公积金利率
      fundRate: {
        value: 0.0325,
        label: "3.25% (最新基准利率1倍)"
      },
      businessMoney: "",//商贷部分金额
      fundMoney: "",//公积金贷部分金额
      customRate: "",//自定义利率
    },
    showSinglePop: false,
    singlePopType: "",
    singlePopTitle: "",
    isOldRate: false,//是否是旧版利率
    isCustomRate: false,//是否自定义利率

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.priceType) {
      this.setData({
        priceType: options.priceType,
        'formData.totalMoney': options.totalPrice,
        'formData.loansMoney': options.totalPrice * 0.7
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  /**响应方法 */

  //贷款总额、房屋总价切换
  switchTabChange(e) {
    console.log("JJLog:顶部切换:" + JSON.stringify(e));
    let item = e.detail.item;
    this.setData({
      priceType: item.key
    })
  },
  //贷款方式切换:
  switchTabLoansChange(e) {
    let item = e.detail.item;
    this.setData({
      loansType: item.key
    })
  },

  //单项输入回调:
  importCellChange(e) {
    console.log("JJLog:单项选择回调:" + JSON.stringify(e));
    let item = e.detail;
    let changeValue = `formData.${item.key}`
    this.setData({
      [changeValue]: item.value
    })
    if (item.key == "totalMoney") {
      //输入房屋总价-贷款额度联动:
      let data = this.data.formData.payRatio;
      data.label = data.value * 100 + "%" + " (" + item.value * data.value + "万" + ")"
      this.setData({
        'formData.loansMoney': item.value * (1 - data.value),
        'formData.payRatio': data
      })
    }
    if (item.key == "fundMoney") {
      //公积金贷款额度输入:
      if (this.data.formData.loansMoney) {
        console.log(this.data.formData.loansMoney);
        //如果总的贷款金额有的情况下:
        if (parseInt(item.value) > this.data.formData.loansMoney) {
          wx.showToast({
            title: '公积金贷款额度大于了贷款总额',
            icon: "none"
          })
          this.setData({
            "formData.fundMoney": "",
          })
        } else if (parseInt(item.value) < this.data.formData.loansMoney) {
          let businessMoney = this.data.formData.loansMoney - item.value;
          this.setData({
            'formData.businessMoney': businessMoney
          })
        }
      }
    }
    if (item.key == "businessMoney") {
      //商贷贷款额度输入:
      if (this.data.formData.loansMoney) {

        //如果总的贷款金额有的情况下:
        if (item.value > this.data.formData.loansMoney) {
          wx.showToast({
            title: '商贷额度大于了贷款总额',
            icon: "none"
          })
          this.setData({
            "formData.businessMoney": "",
          })
        } else if (item.value < this.data.formData.loansMoney) {
          let fundMoney = this.data.formData.loansMoney - item.value;
          this.setData({
            'formData.fundMoney': fundMoney
          })
        }
      }
    }
    if (item.key == "customRate") {
      if (item.value) {
        if (!this.checkNumber(item.value)) {
          wx.showToast({
            title: '自定义利率只能为数字',
            icon: "none"
          })
          this.setData({
            'formData.customRate': "",
          })
        }
      }

    }
  },

  checkNumber(theObj) {
    var reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(theObj)) {
      return true;
    }
    return false;
  },
  //单项选择点击:
  showSinglePop(e) {
    // console.log("JJLog:单项选择出现:" + JSON.stringify(e));
    let type = e.currentTarget.dataset.type;
    let title = "";
    if (type == "rateValue" || type == "oldRate") {
      title = "利率选择"
    }
    if (type == "rateType") {
      title = "利率方式"
    }
    if (type == "fundYear" || type == "businessYear") {
      title = "贷款年限"
    }
    if (type == "fundRate") {
      title = "公积金利率"
    }
    if (type == "payRatio") {
      title = "首付比例"
    }
    this.setData({
      singlePopType: type,
      singlePopTitle: title,
      showSinglePop: true
    })
  },


  //单项选择选中回调:
  singlePopSelect(e) {
    console.log("JJLog:单项选择回调:" + JSON.stringify(e));
    let result = e.detail.result;
    let type = e.detail.type;
    if (type == "rateValue" || type == "oldRate") {
      this.setData({
        'formData.rateValue': result
      })
      if (result.value == "custom") {
        //自定义利率
        this.setData({
          isCustomRate: true,
        })
      } else {
        this.setData({
          isCustomRate: false,
        })
      }
    }
    if (type == "rateType") {
      this.setData({
        'formData.rateType': result
      })
      //利率方式:
      if (result.value == "old") {
        //旧版利率:

        let obj = {
          value: 0.0392,
          label: "3.92% （基准利率8折）"
        }
        this.setData({
          isOldRate: true,
          'formData.rateValue': obj,
          isCustomRate: false
        })

      } else {
        let obj = {
          value: 0.0465,
          label: "利率4.65% (LPR + 0基点)"
        }
        this.setData({
          isOldRate: false,
          'formData.rateValue': obj,
          isCustomRate: false
        })
      }


    }
    if (type == "fundYear") {
      this.setData({
        'formData.fundYear': result
      })
    }
    if (type == "businessYear") {
      this.setData({
        'formData.businessYear': result
      })
    }
    if (type == "fundRate") {
      this.setData({
        'formData.fundRate': result
      })
    }
    if (type == "payRatio") {
      if (this.data.formData.totalMoney) {
        let loansMoney = this.data.formData.totalMoney * (1 - result.value);
        let payRatio = result;

        payRatio.label = result.value * 100 + "%" + " (" + this.data.formData.totalMoney * result.value + "万" + ")"
        this.setData({
          'formData.loansMoney': loansMoney,
          'formData.payRatio': payRatio
        })


      }
    }



    this.singlePopHide();
  },








  //单选弹窗消失:
  singlePopHide() {
    this.setData({
      showSinglePop: false
    })
  },



  //跳转计算详情页:
  showDetail() {
    /*
    loansType:贷款类型 
    businessMoney:商贷金额
    businessRate:商贷利率
    businessYear:商贷年限
    fundMoney:公积金贷金额
    fundRate:公积金贷利率
    fundYear:公积金贷年限
    
    */
    let formData = this.data.formData;
    let loanData = {};
    loanData.loansType = this.data.loansType;
    loanData.businessMoney = this.data.loansType == 2 ? formData.businessMoney : formData.loansMoney;
    loanData.businessYear = formData.businessYear.value;
    loanData.businessRate = this.data.isCustomRate ? formData.customRate : formData.rateValue.value;
    loanData.fundRate = formData.fundRate.value;
    loanData.fundYear = formData.fundYear.value;
    loanData.fundMoney = this.data.loansType == 2 ? formData.fundMoney : formData.loansMoney;

    if (this.data.loansType != 2) {
      //商贷和公积金贷

    }


    let result = JSON.stringify(loanData);
    wx.navigateTo({
      url: '/firstPackage/pages/loansDetailList/loansDetailList?loanData=' + result,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})